package cn.tom.service.impl;

import cn.tom.dao.BookDao;
import cn.tom.entity.Book;
import cn.tom.service.BookService;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class BookServiceImpl implements BookService {

    @Resource
    private BookDao bookDao;
    @Resource
    private RedisTemplate redisTemplate;

    private HashOperations<String,Integer,Object> redisHash(){
        return redisTemplate.opsForHash();
    }

    /**
     * 删除：先删除 数据库，再删除缓存
     * */
    @Override
    public int del(int id) {
        bookDao.del(id);
        Long l = redisHash().delete("t_book",id);
        return l.intValue();
    }

    @Override
    public List<Book> findNewBook(int num) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("idDesc",1);
        map.put("num",num);
        map.put("start",0);
        return bookDao.findPageBook(map);
    }

    @Override
    public List<Book>  findTopSale(int num) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("salenum",1);
        map.put("num",num);
        map.put("start",0);
        return bookDao.findPageBook(map);
    }

    /**
     *  先修改 数据库 ， 再 修改 缓存
     *  缓存 ： 先删除 再 添加
     * */
    @Override
    public int update(Book book) {
        int update = bookDao.update(book);
        if(update >= 1){
            Long aLong = redisHash().delete("t_book", book.getId());
            if(aLong.intValue() >= 1)return aLong.intValue();
            return 0;
        }
        return 0;
    }


    /**
     * 查找单个
     * 这里会发生缓存 穿透
     * */
    @Override
    public Book findOne(int id) {
        Book book = (Book) redisHash().get("t_book",id);
        if(book != null) return book;
        return bookDao.findOne(id);
    }

    @Override
    public int add(Book book) {
        int add = bookDao.add(book);
        if(add >= 1){
            redisHash().put("t_book",book.getId(),book);
            return add;
        }
        return  0 ;
    }

    @Override
    public int countALL() {
        return bookDao.total(0,null);
    }

    @Override
    public List<Book> findBookPages(Integer page, Integer num) {
            page = (page - 1) * num ;
            HashMap<String,Object> map = new HashMap<>();
            map.put("num",num);
            map.put("start",page);
            return  bookDao.findPageBook(map);
    }

    @Override
    public int count(String key) {
        return bookDao.total(0,key);
    }

    @Override
    public List<Book> findPageKey(Integer start, Integer num, String key) {
        start = (start - 1) * num ;
        HashMap<String,Object> map = new HashMap<>();
        map.put("num",num);
        map.put("start",start);
        map.put("key",key);
        List<Book> list = bookDao.findPageBook(map);
        return list;
    }

    @Override
    public List<Book> findPageBook(Integer start,Integer num,Integer typeid) {
        start = (start - 1) * num ;
        HashMap<String,Object> map = new HashMap<>();
        map.put("num",num);
        map.put("start",start);
        map.put("typeid",typeid);
        return bookDao.findPageBook(map) ;
    }

    @Override
    public int total(int typeid) {
        return bookDao.total(typeid,null);
    }
}
